<?php 
define('__ROOT__', dirname(dirname(__FILE__))); 
include_once(__ROOT__.'/includes/header.php'); 
include_once(__ROOT__.'/includes/leftnav.php');
 
if (isset($message)){
	echo '<div class="notification msginfo">';
	echo '<a class="close"></a>';
	echo $message;
	echo '</div>';
}
?>

<div id="content" class="span10">
	<ul class="breadcrumb">
					<li>
						<a href="<?php echo base_url('/home'); ?>">Home</a> <span class="divider">/</span>
					</li>
					<li>
						<a href="<?php echo base_url('/admin/dashboard'); ?>">Dashboard</a> <span class="divider">/</span>
					</li>
					<li>
						<a href="#">Messages</a>
					</li>
	</ul>
	<div class="row-fluid">
		<div class="span6">
			<table class="table table-striped" id="msgTable">
					<tbody>
						<tr>
							<th>Subject</th>
							<th>Posted DateC</th>
							<th>Sender</th>
							<th> Actions</th>
						</tr>
					<?php foreach($messages as $msg): ?>
						<tr id="<?php echo $msg->id; ?>">
							<td><?php echo $msg->subject; ?></td>
							<td><?php echo $msg->posted; ?></td>
							<td><?php echo $msg->firstName; ?></td>
							<td><div class="span2"><a class="broadcastMsg btn" title="view details" id="bcdetails">></a></div></td>
						</tr>
					<?php endforeach; ?>
					</tbody>	
			</table>
		</div>
		<div class="span6">
				<div  id="messageDetails" class="well" style="padding: 8px 10px;">
					<h5>Broadcast message from member </h5>
					<dl class="dl-horizontal">
						<dt style="width: 60px;" class="pull-left">Subject:<dt>
						<dd style="margin-left: 80px;padding-bottom: 10px;" id="subject"></dd>
						<dt style="width: 60px;">Message:</dt>
						<dd style="margin-left: 80px;" id="messagebody"></dd>
					</dl>
			</div>

		</div>
	</div>
	<input type="hidden" id="url" value="<?php echo base_url('/admin/message/details'); ?>">
	</div>
</div>

<?php 
include_once(__ROOT__.'/includes/footer.php');
?>
<script>
//Form submission jqery code under js/external.js file.
//the following code will execute while on page load
$(document).ready(function() {
	//alert('here');
	$("#messageDetails").hide();
	$tr = $("#msgTable tr").eq(1); //grab the second tr which is latest message
	var bid = $tr.attr("id");
	var burl = $('#url').val();
	if (bid){
		var msgData = {
						'id': bid
					}
					$.ajax({
						type: "post",
						url: burl,
						cache: false,
						data: msgData,
						success: function(data) {
							var obj = $.parseJSON(data);
							$("#messageDetails").show();
							$('#subject').text(obj[0].subject);
							$('#messagebody').text(obj[0].body);	
						}
					});
				}
	});

</script>